function th5linkput()

fprintf('\tTesting H5LINKPUT...  ' );
v = version('-release');
switch(v)
    case { '2006b','2007a', '2007b', '2008a', '2008b', '2009a'}
        fprintf('\n\t\tNot available on release %s.\n', v);
		return
end

tfile='testlink6.h5';
gentestfile(tfile);

test_softlinkgroup(tfile);
test_softlinkvar(tfile);

test_hardlinkgroup(tfile);
test_hardlinkvar(tfile);




delete(tfile);

fprintf('OK\n');

end

function gentestfile(fname)

h5id=h5filecreate(fname);
%add a group
h5groupput(h5id,'/testgroup');
%add a second group
h5groupput(h5id,'/testgroup2');
%create a subgroup
h5groupput(h5id,'/testgroup/testb');
%put a random data set
h5dataput(h5id,'/testgroup/test_var1',int8(rand(100,100)*10));
%put a second random data set
h5dataput(h5id,'/testgroup2/test_var2',uint8(rand(100,100)*50));
h5dataput(h5id,'/testgroup2/test_var3','This is a test');
%put an attribute in the file
H5F.close(h5id);
end




function test_softlinkgroup(fname)
%link testgroup2/bob to testgroup with a soft link
% h5linkput(fname,'/testgroup','/testgroup2','linktest1','soft');
% H5G.open(fname,'/testgroup2/linktest1');

% d1b=hdf5read(fname,'/testgroup2/linktest1/var_link');
% d1=h5varget(fname,'/testgroup2/linktest1/var_link');
% d2=h5varget(fname,'/testgroup/test_var1');
% 
% if (~isequal(d1,d2))
%     error('variables do not match');
% end

end


function test_softlinkvar(fname)
%link testgroup2/bob to testgroup with a soft link
h5linkput(fname,'/testgroup2/test_var2','/testgroup/testb/','var_link','soft');

d1=h5varget(fname,'/testgroup/testb/var_link');
d2=h5varget(fname,'/testgroup2/test_var2');

if (~isequal(d1,d2))
    error('variables do not match');
end

end

function test_hardlinkgroup(fname)
%link testgroup2/bob to testgroup with a soft link
h5linkput(fname,'/testgroup','/testgroup2/','linktest2','hard');

d2=h5varget(fname,'/testgroup/test_var1');
d1=h5varget(fname,'/testgroup2/linktest2/test_var1');


if (~isequal(d1,d2))
    error('variables do not match');
end

end


function test_hardlinkvar(fname)
%link testgroup2/bob to testgroup with a soft link
h5linkput(fname,'/testgroup/test_var1','/testgroup2/','var_link_hard','hard');

d1=h5varget(fname,'/testgroup2/var_link_hard');
d2=h5varget(fname,'/testgroup/test_var1');

if (~isequal(d1,d2))
    error('variables do not match');
end

end

